﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace Prototype1.AdminCompany
{
    public partial class WF_DepartmentManagement : System.Web.UI.Page
    {
        private const String TAG = "MYTAG";
        private const String HIDDENFIELD_VALUE = TAG + "_buttonClicked";
        private const String HIDDENFIELD_LEVEL = TAG + "_levelClicked";

        private LinkedList<Department> list;

        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.RegisterHiddenField(HIDDENFIELD_VALUE, "");
            ClientScript.RegisterHiddenField(HIDDENFIELD_LEVEL, "");

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {

                if (IsPostBack && Request.Params[HIDDENFIELD_VALUE] != null)
                    if (Request.Params[HIDDENFIELD_VALUE].Length > 1)
                    {
                        //SOMEONE PUSHED REMOVE BUTTON!!!
                        Department d = new Department();
                        d.id = new Guid(Request.Params[HIDDENFIELD_VALUE]);
                        
                        Department dep = dc.Departments.Single(pc => pc.id == d.id);
                                                
                        dc.Departments.DeleteOnSubmit(dep);
                        dc.SubmitChanges();
                    }


                //PROCEED LOADING LIST

                IEnumerator<Department> enume = null;
            
                var query = from dep in dc.Departments
                            select dep;

                enume = query.GetEnumerator();

                list = new LinkedList<Department>();
                while (enume.MoveNext() && enume.Current != null)
                    list.AddLast(enume.Current);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if(list.Count != 0 )
            {
                this.dep_list_status.Visible = false;
                UpdateDepList();                
            }             
        }

        protected void SaveDepartment(object sender, EventArgs e)
        {
            if (this.tb_dep_name.Text != "")
            {
                Department d = new Department();
                d.id = Guid.NewGuid();
                d.nome = this.tb_dep_name.Text;

                using (DataAccessDataContext dc = new DataAccessDataContext())
                {
                    dc.Departments.InsertOnSubmit(d);
                    dc.SubmitChanges();
                    this.save_status.Text = "Adicionado departamento!";
                    list.AddLast(d);
                }

                ListItem x = new ListItem(d.nome, d.id.ToString());
                if (!this.drop_down_department_list.Items.Contains(x))
                    this.drop_down_department_list.Items.Add(x);

            }
        }

        protected void UpdateDepList()
        {
            this.dep_list.Controls.Add(new LiteralControl("<table>"));
            this.dep_list.Controls.Add(new LiteralControl("<tr>"));
            this.dep_list.Controls.Add(new LiteralControl("<th>Nome</th>"));
            this.dep_list.Controls.Add(new LiteralControl("<th></th>"));
            this.dep_list.Controls.Add(new LiteralControl("</tr>"));


            foreach (Department d in list)
            {
                InsertLine(d);
                if(!IsPostBack)this.drop_down_department_list.Items.Add(new ListItem(d.nome, d.id.ToString()));
            }
            if (!IsPostBack) this.drop_down_department_list.SelectedIndex = 0;
            UpdateLevels();
            this.dep_list.Controls.Add(new LiteralControl("</table>"));
        }


        protected void InsertLine(Department d)
        {
            Button b = new Button();
            b.ID = TAG + d.id;
            b.Text = "Delete";

            String value = "document.forms[0]." + HIDDENFIELD_VALUE + ".value='" + d.id + "';";
            b.Attributes.Add("onClick", value + "document.forms[0].submit();");


            this.dep_list.Controls.Add(new LiteralControl("<tr><td>" + d.nome + "</td><td>"));
            this.dep_list.Controls.Add(b);
            this.dep_list.Controls.Add(new LiteralControl("</td></tr>"));
        }

        protected void UpdateLevels()
        {
            int idx = this.drop_down_department_list.SelectedIndex;
            if (idx > -1)
            {
                
                Guid id = new Guid(this.drop_down_department_list.Items[idx].Value);

                IEnumerator<DepartmentLevel> enume = null;
                using (DataAccessDataContext dc = new DataAccessDataContext())
                {

                    var query = from dep in dc.DepartmentLevels
                                where dep.iddep == id
                                select dep;

                    enume = query.GetEnumerator();

                    enume.MoveNext();


                    if (enume.Current != null)
                    {
                        this.dep_level_list_status.Visible = false;
                        this.dep_level_list.Controls.Add(new LiteralControl("<table>"));
                        this.dep_level_list.Controls.Add(new LiteralControl("<tr>"));
                        this.dep_level_list.Controls.Add(new LiteralControl("<th>Nome</th>"));
                        this.dep_level_list.Controls.Add(new LiteralControl("<th>Nível</th>"));
                        this.dep_level_list.Controls.Add(new LiteralControl("</tr>"));

                        do
                        {
                            InsertLevel(enume.Current);
                        }
                        while (enume.MoveNext());
                    }
                    else
                    {
                        this.dep_level_list_status.Visible = true;
                    }

                    
                }

            }
        }

        protected void InsertLevel(DepartmentLevel dl)
        {
            Button b = new Button();
            b.ID = TAG + dl.idlevel;
            b.Text = "Delete";

            String value = "document.forms[0]." + HIDDENFIELD_LEVEL + ".value='" + dl.idlevel + "';";
            b.Attributes.Add("onClick", value + "document.forms[0].submit();");


            this.dep_level_list.Controls.Add(new LiteralControl("<tr><td>" + dl.nome + "</td><td>" + dl.nivel + "</td><td>"));
            this.dep_level_list.Controls.Add(b);
            this.dep_level_list.Controls.Add(new LiteralControl("</td></tr>"));
        }

        protected void UpdateLevelList(object sender, EventArgs e)
        {

        }

        protected void SaveLevel(object sender, EventArgs e)
        {
            DepartmentLevel d = new DepartmentLevel();
            d.iddep = new Guid(this.drop_down_department_list.SelectedValue);
            d.idlevel = Guid.NewGuid();
            d.nome = this.tb_level_name.Text;
            d.nivel = int.Parse(this.tb_level_level.Text);

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                dc.DepartmentLevels.InsertOnSubmit(d);
                dc.SubmitChanges();                
            }
        }
    }
}
